﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebHoiChoMuaBan.Models;
using WebHoiChoMuaBan.Controllers.Helpers;
using WebHoiChoMuaBan.Controllers.Global;

namespace WebHoiChoMuaBan.Controllers
{
    public class RegisterController: Controller
    {
        //
        // GET: /Register/

        public ActionResult Index()
        {
            return View("~/Views/Home/Register.aspx");
        }

        //
        // GET: /Register/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Register/NewMember

        public ActionResult NewMember()
        {
            return Index();
        } 

        //
        // POST: /Register/NewMember

        [HttpPost]
        public ActionResult NewMember(FormCollection collection)
        {
            // TODO: Add insert logic here
            string pass = collection.Get("Password");
            string conPass = collection.Get("ConPassword");
            if( string.IsNullOrEmpty(pass) || pass.Length < 6)
                ModelState.AddModelError("Password", "Hãy nhập Mật khẩu tối thiếu 6 ký tự.");
            if (!pass.Equals(conPass))
                ModelState.AddModelError("ConPassword", "Hãy nhập mật khẩu 2 lần giống nhau");

            Member mem = new Member();
            mem.MemUsername = collection.Get("Username");
            mem.MemPasswordString = collection.Get("Password");
            mem.MemEmail = collection.Get("Email");
            mem.MemFullname = collection.Get("Fullname");
            mem.MemCellPhone = collection.Get("CellPhone");
            mem.MemJoinDate = DateTime.Now;
            mem.MemAvatar = Config.DEFAULT_AVATAR;

            string[] strChoice;
            bool choice;

            strChoice = collection.Get("Subcribe").Split(',');
            if (!bool.TryParse(strChoice[0], out choice))
            {
                mem.MemSubcribe = false;
            }
            else mem.MemSubcribe = choice;

            strChoice = collection.Get("HideEmail").Split(',');
            if (!bool.TryParse(strChoice[0], out choice))
            {
                mem.MemHideEmail = false;
            }
            else mem.MemHideEmail = choice;

            mem.MemTypeId = 1;
            mem.MemUpCount = mem.MemUpPoint = mem.MemVirPoint = 0;
            mem.MemStatus = (char)Member.Status.Active;

            MemberHelper helperMem = new MemberHelper();
            if (mem.IsValid)
            {
                strChoice = collection.Get("Accept").Split(',');
                if (!bool.TryParse(strChoice[0], out choice) || choice == false)
                {
                    ModelState.AddModelError("Accept", "Để đăng ký thành viên, bạn phải đồng ý với các điều khoản của chúng tôi.");
                }
                else
                {
                    helperMem.Add(mem);
                    helperMem.Save();
                    Response.Redirect("/Home/Login");
                    return null;
                }
            }
            else
            {
                foreach (var issue in mem.GetRuleViolations())
                {
                    ModelState.AddModelError(issue.PropertyName, issue.ErrorMessage);
                }
            }
            
            return View("~/Views/Home/Register.aspx", mem);
        }
        
        //
        // GET: /Register/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Register/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Register/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /Register/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
